
var videoUtils = require('../utils/videoUtils')
const app = getApp()

Page({
  data: {
    faceUrl: "../resource/images/noneface.png",
    isMe: true,
    isFollow:false,

    videoSelClass:"video-info",
    isSelectedWork: "video-info-selected",
    isSelectedLike:"",
    isSelectedFollow:"",

    myVideoList:[],
    myVideoPage:1,
    myVideoTotal:1,

    likeVideoList:[],
    likeVideoPage:1,
    likeVideoTotal:1,

    followVideoList:[],
    followVideoPage:1,
    followVideoTotal:1,

    myWorkFalg:false,
    myLikesFalg:true,
    myFollowFalg:true

  },  
  onLoad:function(params){
    // var user = app.userInfo
    // 修改原有的全局对象为本地缓存
    var user = app.getGloalUserInfo();
    var serverUrl = app.serverUrl;
    var userId = user.id;
    var me = this

    var publisherId = params.publisherId;
    
    if (publisherId != null && publisherId != undefined && publisherId != ''){
      userId = publisherId;
      me.setData({
        isMe:false,
        publisherId:publisherId,
      })
    }

    me.setData({
      userId:userId
    })
    wx.showLoading({
      title: '请等待',
    })

    wx.request({
      url: serverUrl + "/user/query?userId=" + userId + "&fanId=" + user.id,
      method:"POST",
      header:{
        "content-type": "application/json",
        'userId':user.id,
        'userToken': user.userToken
      },
      
      success:function(res){
        wx.hideLoading();
        if (res.data.status == 200){
          var userInfo = res.data.data;
          if ( userInfo.faceImage != null &&  userInfo.faceImage!='' &&  userInfo.faceImage != undefined){
            var faceUrl = serverUrl + userInfo.faceImage
          }else{
            var faceUrl =  "../resource/images/noneface.png"
          }
          me.setData({
            faceUrl:faceUrl,
            fansCounts:userInfo.fansCounts,
            followCounts: userInfo.followCounts,
            receiveLikeCounts:userInfo.receiveLikeCounts,
            nickname:userInfo.nickname,
            //后端字段时isFollow传给前端的数据会变成follow
            isFollow:userInfo.follow
          })
        }else if(res.data.status == 502){
          var realUrl = "../mine/mine"
          wx.showToast({
            title: res.data.msg,
            duration: 3000,
            icon: "none",
            success:function(){
              wx.redirectTo({
                url: '../userLogin/login?redirectUrl=' + realUrl,
              })
            }
          })
        }
      }
    })

    this.getMyVideoList(1);
  },

  logout:function(e){
    // var user = app.userInfo
    // 修改原有的全局对象为本地缓存
    var user = app.getGloalUserInfo();
    var userId = user.id;

    wx.request({
      url: app.serverUrl + "/logout?userId=" + userId,
      method:"POST",
      header:{
        "content-type": "application/json"
      },
      success:function(res){
        var realUrl = "../mine/mine"
        if (res.data.status == 200){
          wx.showToast({
            title: '用户注销成功',
          })
          //删除用户信息
          // app.userInfo=null;
          //注销以后清空缓存
          wx.removeStorageSync('userInfo');
          //跳转登录页面
          wx.navigateTo({
            url: '../userLogin/login?redirectUrl=' + realUrl,
          })
        }
      }
    })
  },
  changeFace:function(e){
    var me = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;

        wx.showLoading({
          title:"上传中..."
        })

        var serverUrl = app.serverUrl
        // 修改原有的全局对象为本地缓存
        var userInfo = app.getGloalUserInfo();
        wx.uploadFile({
          filePath: tempFilePaths[0],
          name: 'file',
          url: serverUrl + "/user/uploadFace?userId=" + userInfo, //app.userInfo.id,
          header:{
            "content-type": "application/json"
          },
          success:function(res){
            var data = JSON.parse(res.data);
            console.log(data)
            wx.hideLoading();
            if (data.status == 200){
              wx.showToast({
                title: '上传成功~',
                icon: "success",
                duration:2000
              })
              //页面头像展示
              var imageUrl = data.data;
              me.setData({
                faceUrl: serverUrl + imageUrl
              })
            }else if(data.status == 500){
                wx.showToast({
                title: res.data.msg,
                duration:2000
              })
            }
          } 
        })
      }
    })
  },

  uploadVideo:function(){
    // videoUtils.uploadVideo(); 可以使用该方法
    var me = this;
    wx.chooseVideo({
      sourceType: ['album'],
      success(res) {
        console.log(res)
        var duration = res.duration;
        var tempHeight = res.height;
        var tempWidth = res.width;
        var tmpVideoUrl = res.tempFilePath;
        var tmpCoverUrl= res.thumbTempFilePath;
  
        if (duration > 10){
          wx.showToast({
            title: '视频长度不能超过10秒',
            icon:"none",
            duration:2500
          })
        }else if(duration < 1){
          wx.showToast({
            title: '视频长度太短，请上传大于1秒的视频',
            icon:"none",
            duration:2500
          })
        }else{
          //页面跳转
          wx.navigateTo({
            url: '../chooseBgm/chooseBgm?duration=' + duration
            + "&tempHeight=" + tempHeight
            + "&tempWidth=" + tempWidth
            + "&tmpVideoUrl=" + tmpVideoUrl
            + "&tmpCoverUrl=" + tmpCoverUrl,
          })
        }
      }
    })
  },

  followMe:function(e){
    var me = this;
    var publisherId = me.data.publisherId;
    var user = app.getGloalUserInfo();
    var userId = user.id;

    var follwType = e.currentTarget.dataset.follwtype;
    var url = '';
    //1:关注
    //0:取消关注
    if (follwType == '1'){
      url = '/user/beYourFans?userId=' + publisherId + "&fanId=" + userId;
    }else{
      url = '/user/dontBeYourFans?userId=' + publisherId + "&fanId=" + userId;
    }

    wx.showLoading();

    wx.request({
      url: app.serverUrl + url,
      method:'POST',
      header:{
        "content-type": "application/json",
        'userId':user.id,
        'userToken': user.userToken
      },
      success:function(){
        wx.hideLoading();
        if (follwType == '1'){
          me.setData({
            isFollow:true,
            fansCounts: ++me.data.fansCounts
          })
        }else{
          me.setData({
            isFollow:false,
            fansCounts: --me.data.fansCounts
          })
        }
      }
    })
  },

  doSelectWork:function(){
    this.setData({
      isSelectedWork:"video-info-selected",
      isSelectedLike:"",
      isSelectedFollow:"",
      //设置隐藏
      myWorkFalg:false,
      myLikesFalg:true,
      myFollowFalg:true,

      myVideoList:[],
      myVideoPage:1,
      myVideoTotal:1,
  
      likeVideoList:[],
      likeVideoPage:1,
      likeVideoTotal:1,
  
      followVideoList:[],
      followVideoPage:1,
      followVideoTotal:1,
    })
    this.getMyVideoList(1);
  },

  getMyVideoList:function(page){
    var me = this;
    var serverUrl = app.serverUrl;

    wx.showLoading();
    wx.request({
      url: serverUrl + "/video/showAll?page=" + page + "&pageSize=6",
      method:"POST",
      data:{
      userId:me.data.userId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:function(res){
        wx.hideLoading();
        console.log(res)
        var myVideoList = res.data.data.rows;
        
        var newVideoList = me.data.myVideoList;
        
        me.setData({
          myVideoList:newVideoList.concat(myVideoList),
          myVideoPage: page,
          myVideoTotal: res.data.data.total,
          serverUrl: app.serverUrl
        })
      }
    })
  },

  doSelectLike:function(){
    this.setData({
      isSelectedWork:"",
      isSelectedLike:"video-info-selected",
      isSelectedFollow:"",

      myWorkFalg:true,
      myLikesFalg:false,
      myFollowFalg:true,

      myVideoList:[],
      myVideoPage:1,
      myVideoTotal:1,
  
      likeVideoList:[],
      likeVideoPage:1,
      likeVideoTotal:1,
  
      followVideoList:[],
      followVideoPage:1,
      followVideoTotal:1,
    })
    this.getMyLikesList(1)
  },

  getMyLikesList:function(page){
    var me = this;
    var serverUrl = app.serverUrl;
    var userId = me.data.userId;
    wx.showLoading();
    wx.request({
      url: serverUrl + "/video/showMyLike?userId=" + userId + "&page=" + page + "&pageSize=6",
      method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:function(res){
        wx.hideLoading();
        console.log(res)
        var likeVideoList = res.data.data.rows;
        
        var newVideoList = me.data.likeVideoList;
        
        me.setData({
          likeVideoList:newVideoList.concat(likeVideoList),
          likeVideoPage: page,
          likeVideoTotal: res.data.data.total,
          serverUrl: app.serverUrl
        })
      }
    })
  },
  
  doSelectFollow:function(){
    this.setData({
      isSelectedWork:"",
      isSelectedLike:"",
      isSelectedFollow:"video-info-selected",

      myWorkFalg:true,
      myLikesFalg:true,
      myFollowFalg:false,

      myVideoList:[],
      myVideoPage:1,
      myVideoTotal:1,
  
      likeVideoList:[],
      likeVideoPage:1,
      likeVideoTotal:1,
  
      followVideoList:[],
      followVideoPage:1,
      followVideoTotal:1,
    })
    this.getMyFollowList(1)
  },

  getMyFollowList:function(page){
    var me = this;
    var serverUrl = app.serverUrl;
    var userId = me.data.userId;
    wx.showLoading();
    wx.request({
      url: serverUrl + "/video/showMyFollow?userId=" + userId + "&page=" + page + "&pageSize=6",
      method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:function(res){
        wx.hideLoading();
        console.log(res)
        var followVideoList = res.data.data.rows;
        
        var newVideoList = me.data.followVideoList;
        
        me.setData({
          followVideoList:newVideoList.concat(followVideoList),
          followVideoPage: page,
          followVideoTotal: res.data.data.total,
          serverUrl: app.serverUrl
        })
      }
    })
  },

  onReachBottom:function(){
    var myWorkFalg = this.data.myWorkFalg;
    var myLikesFalg = this.data.myLikesFalg;
    var myFollowFalg = this.data.myFollowFalg;

    if(!myWorkFalg){
      var currentPage = this.data.myVideoPage;
      var totalPage = this.data.myVideoTotal;
      //获取总页数进行判断，如果当前页数和总页数相等。则不分页
      if (currentPage == totalPage){
        wx.showToast({
          title: '已经没视频啦...',
          icon:"none"
        });
        return;
      }
      var page = currentPage + 1;
      this.getMyVideoList(page);
    }else if(!myLikesFalg){
      var currentPage = this.data.likeVideoPage;
      var totalPage = this.data.likeVideoTotal;
      //获取总页数进行判断，如果当前页数和总页数相等。则不分页
      if (currentPage == totalPage){
        wx.showToast({
          title: '已经没视频啦...',
          icon:"none"
        });
        return;
      }
      var page = currentPage + 1;
      this.getMyLikesList(page);
    }else if(!myFollowFalg){
      var currentPage = this.data.followVideoPage;
      var totalPage = this.data.followVideoTotal;
      //获取总页数进行判断，如果当前页数和总页数相等。则不分页
      if (currentPage == totalPage){
        wx.showToast({
          title: '已经没视频啦...',
          icon:"none"
        });
        return;
      }
      var page = currentPage + 1;
      this.getMyFollowList(page);
    }
  },

  showVideo:function(e){
    var myWorkFalg = this.data.myWorkFalg;
    var myLikesFalg = this.data.myLikesFalg;
    var myFollowFalg = this.data.myFollowFalg;

    if(!myWorkFalg){
      var videoList = this.data.myVideoList;
    }else if(!myLikesFalg){
      var videoList = this.data.likeVideoList;
    }else if(!myFollowFalg){
      var videoList = this.data.followVideoList;
    }
    var arrIndex = e.target.dataset.arrindex;
    var videoInfo = JSON.stringify(videoList[arrIndex]);

    wx.navigateTo({
      url: '../videoInfo/videoInfo?videoInfo=' + videoInfo,
    })
  }
})
